<script setup>
import { onMounted, ref } from "vue";
import { tipsHook } from "@/utils/tipsHook";
import WebSocketService from "@/utils/websocket";
import chatList from "./chatList/index.vue";
import V3Emoji from "vue3-emoji";

const { tipMsg, noticeMsg } = tipsHook();

// 用户输入内容
const messageText = ref("");
// 聊天数据
let chatHistory = ref([]);
// websocket 实例
const ws = ref();

// 随机生成用户名称和uid 模仿用户数据
let names = ["刘一", "陈二", "张三", "李四", "王五", "赵六"];
const userInfo = {
  name: names[Math.floor(Math.random() * (0 - 5) + 5)],
  // name: '用户' + Math.floor(Math.random() * (0 - 10000) + 10000),
  unicode: Math.floor(Math.random() * (0 - 10000) + 10000),
};

const initWebSocket = () => {
  ws.value = new WebSocketService(
    "ws://localhost:8080",
    userInfo,
    tipMsg,
    noticeMsg,
    chatHistory
  );
  ws.value.connect();
};

onMounted(() => {
  initWebSocket();
});

// 发送消息
const sendMsg = () => {
  let sendData = {
    // 1 系统通知 2 用户聊天信息
    type: 2,
    name: userInfo.name,
    uid: userInfo.unicode,
    message: messageText.value,
  };
  ws.value.send(sendData);
  messageText.value = "";
};



const optionsName = {
  "Smileys & Emotion": "笑脸&表情",
  "Food & Drink": "食物&饮料",
  "Animals & Nature": "动物&自然",
  "Travel & Places": "旅行&地点",
  "People & Body": "人物&身体",
  Objects: "物品",
  Symbols: "符号",
  Flags: "旗帜",
  Activities: "活动"
}
const clickEmoji = (val) => {
  messageText.value += val;
};
</script>

<template>
  <div class="pageWrapper">
    <div class="titleArea">websocketDemo</div>
    <chatList :userInfo="userInfo" :dataList="chatHistory" />
    <div class="operateArea">
      <input class="iptBox" type="text" v-model="messageText" />
      <div class="emojiBox">
        <V3Emoji
          @click-emoji="clickEmoji"
          :recent="true"
          size="small"
          :options-name="optionsName"
        ></V3Emoji>
      </div>
      <div class="sendBtn" @click="sendMsg">发送</div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep(.emojiBox) {
  width: fit-content;
  margin: 0 10px;
}

:deep(.emojiBox p) {
  margin-left: 10px !important;
}


.pageWrapper {
  box-sizing: border-box;
  width: 100%;
  max-width: 600px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  background: #fff;
  overflow: hidden;
  margin: 20px;

  .titleArea {
    height: 60px;
    line-height: 60px;
    font-weight: bold;
    font-size: 24px;
    color: #fff;
    background: #007bff;
    border-bottom: 4px solid #0056b3;
    border-radius: 10px 10px 0 0;
    text-align: center;
  }

  .operateArea {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: #f9f9f9;
    border-top: 4px solid #0056b3;

    .iptBox {
      padding: 10px;
      flex: 1;
      height: 40px;
      border: 2px solid #007bff;
      border-radius: 20px;
      color: #333;
      background: #fff;
      font-size: 16px;
      outline: none;
      margin-right: 10px;
    }

    .sendBtn {
      padding: 10px 20px;
      height: 40px;
      border-radius: 20px;
      color: #fff;
      font-size: 16px;
      font-weight: bold;
      background-color: #007bff;
      cursor: pointer;
      transition: background-color 0.25s;

      &:hover {
        background-color: #0056b3;
      }
    }
  }
}
</style>
